home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / DCLAP 4j / DBio / DSeqChildApp.cpp < prev    next >
Encoding:
Text File  |  1995-12-17  |  12.2 KB  |  455 lines  |  [TEXT/R*ch]

  1. // DSeqChildApp.cpp
  2. // d.g.gilbert
  3.  
  4.  
  5. #include "DSeqChildApp.h"
  6.  
  7. #include <ncbi.h>
  8. #include <dgg.h>
  9. #include <Dvibrant.h>
  10. #include <DControl.h>
  11. #include <DApplication.h>
  12. #include <DTask.h>
  13. #include <DMenu.h>
  14. #include <DPanel.h>
  15. #include <DUtil.h>
  16. #include <DWindow.h>
  17. #include <DChildApp.h>
  18.  
  19. #include "DSeqFile.h"
  20. #include "DSeqList.h"
  21. #include "DSeqDoc.h"
  22.  
  23.  
  24. #if 0
  25.  need 3 dialog methods here --
  26.   * run app -- simple dlog w/ description, run button, help button, ?cancel button
  27.   * edit child list -- list dlog w/ list of child apps, edit button, delete button, add button
  28.   * edit child -- dlog w/ child app params (pathname, desc, cmdline,
  29.                      in/out files, min seqs, seqformat, other??)
  30.  
  31. #endif
  32.  
  33.  
  34. class DSeqChildApp : public DChildApp
  35. {
  36. public:
  37.     //char  * fName, * fCmdline;
  38.     //const char * fStdin, * fStdout, * fStderr;
  39.     enum seqactions { kOpenSequence = 11, kOpenTree };
  40.     char     * fMenuname, * fDescription, * fSeqformat, *fHelpfile;
  41.     short        fMinseq, fMenucmd;
  42.     DFile * fInfile;
  43.     
  44.     DSeqChildApp() :
  45.         DChildApp(),
  46.         fMenuname(NULL), fDescription(NULL), fSeqformat(NULL), fHelpfile(NULL),
  47.         fInfile(NULL), fMinseq(0), fMenucmd(0)
  48.         {
  49.             fFiles= new DList();
  50.         }
  51.     virtual ~DSeqChildApp();
  52.     virtual void LaunchDialog( DSeqList* aSeqList); 
  53.     virtual void FileAction( DChildFile* aFile);
  54. };
  55.  
  56. class DLaunchDialog : public DWindow {
  57.     enum { cHELP = 3222 };
  58.     char *fHelpfile;
  59.     DSeqChildApp* fChild;
  60. public: 
  61.     DLaunchDialog( DSeqChildApp* child);
  62.     virtual void Open();            
  63.     virtual Nlm_Boolean IsMyAction(DTaskMaster* action); 
  64. };
  65.         
  66.         
  67.         
  68. DLaunchDialog::DLaunchDialog( DSeqChildApp* child) :
  69.     DWindow( 0, gApplication, fixed, -10, -10, -50, -20, "Launch app"),
  70.     fChild(child)
  71.     {
  72.     }
  73.  
  74. void DLaunchDialog::Open()
  75. {
  76.     char buf[128];
  77.     
  78.     if (fChild->fDescription) { 
  79.         DCluster* clu= new DCluster( 0, this, 0, 0, false, "Description");   //0,0 == wid,hi
  80.         new DNotePanel(0, clu, fChild->fDescription, 200, 50);    
  81.         this->NextSubviewBelowLeft();
  82.         }
  83.     sprintf( buf, "Launch %s?",fChild->fMenuname);
  84.     new DPrompt(0, this, buf, 0, 0, Nlm_systemFont);             
  85.     this->NextSubviewBelowLeft();
  86.             
  87.     this->AddOkayCancelButtons(cOKAY,"Launch",cCANC,"Cancel");
  88.     if (fChild->fHelpfile) {
  89.         fHelpfile= fChild->fHelpfile;
  90.         this->NextSubviewToRight();
  91.         new DButton( cHELP, this, "Help");
  92.         }
  93.     else
  94.         fHelpfile= NULL;
  95.         
  96.     DWindow::Open();
  97. }
  98.  
  99. Nlm_Boolean DLaunchDialog::IsMyAction(DTaskMaster* action) 
  100. {    
  101.     switch(action->Id()) {
  102.         case cHELP:
  103.             gApplication->OpenDocument(fHelpfile);
  104.             return true;
  105.         default:
  106.             return DWindow::IsMyAction(action);    
  107.         }
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. DSeqChildApp::~DSeqChildApp()
  115. {
  116.     MemFree( fMenuname);
  117.     MemFree( fDescription);
  118.     MemFree( fSeqformat);
  119.     MemFree( fHelpfile);
  120. }
  121.  
  122. void DSeqChildApp::LaunchDialog( DSeqList* aSeqList) 
  123. {
  124.     DLaunchDialog* win= NULL;
  125.     Boolean okay;
  126.     
  127.     if (fMinseq>0 && (!aSeqList || aSeqList->GetSize()<fMinseq)) {
  128.         Message(MSG_OK, "%s needs a selection of at least %d sequences", 
  129.                             fMenuname, fMinseq);
  130.         return;
  131.         }
  132.  
  133. #ifdef WIN_MAC
  134.         // This works for MAC but fails for MSWIN && UNIX!
  135.     win= new DLaunchDialog(this);
  136.     okay= win->PoseModally();
  137.     delete win;  // for MSWin, MUST delete win BEFORE LAUNCH !! -- now mswin fails here
  138. #else
  139. #if defined( NOT_WIN_MOTIF ) || defined( NOT_WIN_MSWIN)
  140.     // bad problems in Unix/XWin w/ this dialog mangling child launch process...
  141.     okay= true;
  142. #else        
  143.         // this works for MSWIN but fails on MAC !
  144.     DLaunchDialog ldlog(this);
  145.     okay= ldlog.PoseModally();
  146. #endif
  147. #endif
  148.  
  149.  if (okay) {        
  150.                 // write aSeqList to this->inputfile...
  151.         DFile* aFile = this->fInfile;
  152.         if (aFile) {
  153.             aFile->Open("w");
  154.             short  outformat= DSeqFile::FormatFromName(fSeqformat);
  155.             if (outformat == DSeqFile::kUnknown) 
  156.                 outformat= DSeqFile::kPearson; //?? or kPearson ?
  157.             aSeqList->ClearSelections(); //! make sure we write all of seq from this call !?
  158.             aSeqList->DoWrite( aFile, outformat);
  159.              // ?? delete aSeqList if we own it !?
  160.             //aFile->WriteData( buffer, buflen);
  161.             aFile->Close();
  162.             aFile->SetMode("r");
  163.             }
  164.         
  165.         if (!this->Launch())
  166.             Message(MSG_OK, "Failed to launch %s with '%s'", fName, fCmdline);
  167.       }
  168. }
  169.  
  170.  
  171. void DSeqChildApp::FileAction( DChildFile* aFile)
  172. {
  173.     switch (aFile->fAction) {
  174.       case kOpenSequence:
  175.             DSeqDoc::fgTestSeqFile= true; // ???
  176.             gApplication->OpenDocument( aFile);
  177.             DSeqDoc::fgTestSeqFile= false; 
  178.             break;
  179.         
  180.       case kOpenTree:
  181.           // fix later...
  182.         default:
  183.             DChildApp::FileAction( aFile);
  184.             break;
  185.         }
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192. // class DSeqApps : public DObject
  193.  
  194.  
  195. // static
  196. void DSeqApps::CallChildApp(short menucmd, DSeqList* aSeqList) 
  197. {
  198.     DSeqChildApp * child = NULL;
  199.     short i, n = DChildAppManager::gChildList->GetSize();
  200.     for (i= 0; i<n; i++) {
  201.         child= (DSeqChildApp*) DChildAppManager::gChildList->At(i);
  202.         if (child->fMenucmd == menucmd) {
  203.             child->LaunchDialog( aSeqList);
  204.             return;
  205.             }
  206.         }
  207.     Message(MSG_OK, "Child app not found");
  208. }
  209.  
  210.  
  211.  
  212. // static
  213. void DSeqApps::SetUpMenu(short menuId, DMenu*& aMenu) 
  214. {
  215.     char  * kAppSection = "apps";
  216.     long     atsection;
  217.     ulong sectlen = 0;
  218.     char    * value, * item, * appsection, * appvalues, * sections,
  219.                 * newpath,  * cp;
  220.     short menuitem= kChildMenuBaseID;
  221.     DSeqChildApp     * child;
  222.     DChildFile         * cfile;
  223.     Boolean     noStderr= true, noStdout= true, noStdin= true;
  224.     
  225.     
  226.     //DWindow::SetUpMenu(menuId, aMenu);
  227.     if (!aMenu) aMenu = gApplication->NewMenu( menuId, "ChildApps");
  228.  
  229.     sections= gApplication->GetPrefSection( kAppSection, sectlen);
  230.     atsection= 0;
  231.     while (atsection<sectlen) {
  232.         appsection= sections + atsection;
  233.         if (appsection) {
  234.             long     atvalue;
  235.             ulong applen= 0;
  236.             
  237.             child= new DSeqChildApp();
  238.             // child is stored in DChildAppManager::gChildList
  239.             
  240.             value= gApplication->GetPref( appsection, kAppSection);
  241.             if (!value) value= StrDup( appsection);
  242.             aMenu->AddItem( menuitem, value);
  243.             child->fMenuname= value;
  244.             child->fMenucmd= menuitem;
  245.             menuitem++;
  246.     
  247.             child->fReusable= true; // don't delete after one use...
  248.         
  249.             appvalues= gApplication->GetPrefSection( appsection, applen);
  250.             atvalue= 0;
  251.             while (atvalue<applen) {
  252.                 item= appvalues + atvalue;
  253.                 value= gApplication->GetPref( item, appsection);
  254.                 
  255.                 if (!value)
  256.                     ;
  257.                 else if (StrICmp(item,"desc")==0)  
  258.                     child->fDescription= value;
  259.                 else if (StrICmp(item,"path")==0) {
  260.                     cp= value; while (isspace(*cp) || *cp == '$') cp++;
  261.                     child->fName= gApplication->ConvertStdFilePath(cp);
  262.                     MemFree( value);
  263.                     }
  264.                 else if (StrICmp(item,"help")==0) {
  265.                     cp= value; while (isspace(*cp) || *cp == '$') cp++;
  266.                     child->fHelpfile= gApplication->ConvertStdFilePath(cp);
  267.                     MemFree( value);
  268.                     }
  269.                     
  270.                 else if (StrICmp(item,"seqformat")==0)     
  271.                     child->fSeqformat= value;
  272.                 else if (StrICmp(item,"minseq")==0) {
  273.                     child->fMinseq= atoi(value);
  274.                     MemFree( value);
  275.                     }
  276.             
  277.                 else if (StrICmp(item,"cmd")==0)    {
  278.                     // convert variables of form $word[delimiter] ...
  279.                     // especially file path variables
  280.                     char * ep;
  281.                     char * newvalue = StrDup("");
  282.                     cp = value;
  283.                     do {
  284.                         ep= StrChr( cp, '$');
  285.                         if (ep) {
  286.                             *ep++ = 0;
  287.                             StrExtendCat( &newvalue, cp);  // add on string before '$'
  288.                             
  289.                                 // find & substitute any pathname
  290.                             newpath= gApplication->ConvertStdFilePath(ep);
  291.                                 // newpath is all of remaining string ... cut pathname from remainder of str
  292.                             char* np= newpath;
  293.                             while (isgraph(*np)) np++;  *np= 0;
  294.                             StrExtendCat( &newvalue, newpath);
  295.                             MemFree( newpath);
  296.                             
  297.                                 // locate next part of str to parse -- cue on whitespace
  298.                             while (isgraph(*ep)) ep++;  
  299.                             cp= ep;
  300.                             }
  301.                         else
  302.                             StrExtendCat( &newvalue, cp);
  303.                     } while (ep);
  304.                     MemFree( value);
  305.                     value= newvalue;
  306.                     child->fCmdline= value;
  307.                     }
  308.                     
  309.                     // need also to handle dialog control items !!, e.g., dlog=control spec...
  310.                     // and need to handle dlog control substitutions in cmdline...
  311.                             
  312.                 else if (StrNICmp(item,"infile",6)==0) {
  313.                     cp= value; while (isspace(*cp) || *cp == '$') cp++;
  314.                     newpath= gApplication->ConvertStdFilePath(cp);
  315.                     cfile= new DChildFile( newpath, DChildFile::kInput, DChildFile::kDeleteWhenDone,
  316.                                                                     DChildFile::kNoAction, "r", "TEXT", "Spup");
  317.                     child->AddFile(cfile);
  318.                     child->fInfile= cfile; // ?? can we have more than one infile ??
  319.                     MemFree( newpath);
  320.                     MemFree( value);
  321.                     }
  322.  
  323.                 else if (StrNICmp(item,"stdin",5)==0) {
  324.                     noStdin= false;
  325.                     cp= value; while (isspace(*cp) || *cp == '$') cp++;
  326.                     newpath= gApplication->ConvertStdFilePath(cp);
  327.                     cfile= new DChildFile( newpath, DChildFile::kStdin, DChildFile::kDontDelete,
  328.                                                                     DChildFile::kNoAction, "r", "TEXT", "Spup");
  329.                     child->AddFile(cfile);
  330.                     MemFree( newpath);
  331.                     MemFree( value);
  332.                     }
  333.  
  334.                 else if (StrNICmp(item,"stderr",6)==0) {
  335.                     noStderr= false;
  336.                     short action = DChildFile::kOpenText;
  337.                     char* ftype= StrChr(value,'\t'); // look for mime/type after name
  338.                     if (!ftype) ftype= StrChr(value,' '); // !? can we also scan for spaces ??
  339.                     if (ftype) {
  340.                         *ftype++= 0;
  341.                         while (*ftype && *ftype <= ' ') ftype++;
  342.                         if (StrNICmp(ftype,"biosequence",11) == 0 
  343.                          || StrNICmp(ftype,"sequence",8) == 0)
  344.                                 action= DSeqChildApp::kOpenSequence;
  345.                         else if (StrNICmp(ftype,"biotree",7) == 0 
  346.                          || StrNICmp(ftype,"tree",4) == 0)
  347.                                 action= DSeqChildApp::kOpenTree;
  348.                         }
  349.                     cp= value; while (isspace(*cp) || *cp == '$') cp++;
  350.                     newpath= gApplication->ConvertStdFilePath(cp);
  351.                     cfile= new DChildFile( newpath, DChildFile::kStderr, DChildFile::kDontDelete,
  352.                                                                     action, "r", "TEXT", "Spup");
  353.                     child->AddFile(cfile);
  354.                     MemFree( newpath);
  355.                     MemFree( value);
  356.                     }
  357.  
  358.                 else if (StrNICmp(item,"stdout",6)==0) {
  359.                     noStdout= false;
  360.                     short action = DChildFile::kOpenText;
  361.                     char* ftype= StrChr(value,'\t'); // look for mime/type after name
  362.                     if (!ftype) ftype= StrChr(value,' '); // !? can we also scan for spaces ??
  363.                     if (ftype) {
  364.                         *ftype++= 0;
  365.                         while (*ftype && *ftype <= ' ') ftype++;
  366.                         if (StrNICmp(ftype,"biosequence",11) == 0 
  367.                          || StrNICmp(ftype,"sequence",8) == 0)
  368.                                 action= DSeqChildApp::kOpenSequence;
  369.                         else if (StrNICmp(ftype,"biotree",7) == 0 
  370.                          || StrNICmp(ftype,"tree",4) == 0)
  371.                                 action= DSeqChildApp::kOpenTree;
  372.                         }
  373.                     cp= value; while (isspace(*cp) || *cp == '$') cp++;
  374.                     newpath= gApplication->ConvertStdFilePath(cp);
  375.                     cfile= new DChildFile( newpath, DChildFile::kStdout, DChildFile::kDontDelete,
  376.                                                                     action, "r", "TEXT", "Spup");
  377.                     child->AddFile(cfile);
  378.                     MemFree( newpath);
  379.                     MemFree( value);
  380.                     }
  381.                     
  382.                 else if (StrNICmp(item,"outfile",7)==0) {
  383.                     // combine stdout & stderr w/ this code...
  384.                     short action = DChildFile::kOpenText;
  385.                     char* ftype= StrChr(value,'\t'); // look for mime/type after name
  386.                     if (!ftype) ftype= StrChr(value,' '); // !? can we also scan for spaces ??
  387.                     if (ftype) {
  388.                         *ftype++= 0;
  389.                         while (*ftype && *ftype <= ' ') ftype++;
  390.                         if (StrNICmp(ftype,"biosequence",11) == 0 
  391.                          || StrNICmp(ftype,"sequence",8) == 0)
  392.                                 action= DSeqChildApp::kOpenSequence;
  393.                         else if (StrNICmp(ftype,"biotree",7) == 0 
  394.                          || StrNICmp(ftype,"tree",4) == 0)
  395.                                 action= DSeqChildApp::kOpenTree;
  396.                         }
  397.                     cp= value; while (isspace(*cp) || *cp == '$') cp++;
  398.                     newpath= gApplication->ConvertStdFilePath(cp);
  399.                     cfile= new DChildFile( newpath, DChildFile::kOutput, DChildFile::kDontDelete,
  400.                                                                     action, "r", "TEXT", "Spup");
  401.                     child->AddFile(cfile);
  402.                     MemFree( newpath);
  403.                     MemFree( value);
  404.                     }
  405.                     
  406.             
  407.                 atvalue += StrLen(item)+1;
  408.                 }
  409.             
  410.             if (noStderr) child->AddFile(DChildFile::kStderr, NULL);
  411.             if (noStdout) child->AddFile(DChildFile::kStdout, NULL);
  412.             
  413.             MemFree( appvalues);
  414.             }
  415.         atsection += StrLen(appsection)+1;
  416.         }
  417.     MemFree( sections);
  418. }
  419.  
  420.  
  421.  
  422.  
  423. #if 0
  424.  
  425.             /////// prefs file example ///////
  426. [apps]
  427. ;localvar=menu title
  428. clustal=Multiple align...
  429. cap=Contig assembly...
  430. dnaml=Phylip DNAML...
  431.  
  432. [clustal]
  433. ;file path to application 
  434. path=apps:clustalw
  435. help=apps:clustalw_help
  436. ;description for dialog
  437. desc=A multiple sequence alignment application
  438. ;dialog controls (HTML forms format?)
  439.  
  440. ;command line switches
  441. cmd=/infile=temp:clustal.inseq /outfile=clustal.outseq /output=gcg /align
  442.  
  443. ;sequence selection to input file 
  444. infile=clustal.inseq
  445. ;sequence format to send to app
  446. seqformat=nbrf
  447. ;minimum number of selected sequences to activate this command
  448. minseq=2
  449. ;return data files, with doc/type
  450. outfile1=clustal.outseq    text/plain
  451. outfile2=clustal.dnd    text/plain
  452.  
  453.  
  454. #endif
  455.